package king.dominic.jlibrary.util;

import android.os.Handler;

import java.lang.ref.WeakReference;

/**
 * Created by Administrator on 2017/11/8.
 * VerificationCodeTimer
 */

public class VerificationCodeTimer {
    private WeakReference<PerSecondCallback> mPerSecondCallbackWeakRef;
    private WeakReference<FinishCallback> mFinishCallbackWeakRef;
    private Handler mHandler = new Handler();
    private int mDefaultTime = 120;
    private int time = 0;
    private static final int DELAYED = 1000;

    private VerificationCodeTimer(Builder builder) {
        mPerSecondCallbackWeakRef = builder.mPerSecondCallbackWeakRef == null ? new WeakReference<PerSecondCallback>(null) : builder.mPerSecondCallbackWeakRef;
        mFinishCallbackWeakRef = builder.mFinishCallbackWeakRef == null ? new WeakReference<FinishCallback>(null) : builder.mFinishCallbackWeakRef;
        mDefaultTime = builder.mDefaultTime;
    }

    public boolean isStart(){
        return time>0;
    }

    public void start() {
        mHandler.removeCallbacks(runnable);
        time = mDefaultTime;
        runnable.run();
    }

    public void stop() {
        mHandler.removeCallbacks(runnable);
        time = 0;
        runnable.run();
    }

    public void destroy() {
        stop();
        if (mPerSecondCallbackWeakRef != null)
            mPerSecondCallbackWeakRef.clear();
        if (mFinishCallbackWeakRef != null)
            mFinishCallbackWeakRef.clear();
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            PerSecondCallback perSecondCallback = mPerSecondCallbackWeakRef.get();
            FinishCallback finishCallback = mFinishCallbackWeakRef.get();
            if (--time <= 0) {
                if (perSecondCallback != null) perSecondCallback.onPerSecond(0);
                if (finishCallback != null) finishCallback.onFinish();
            } else {
                if (perSecondCallback != null) perSecondCallback.onPerSecond(time);
            }
            if (time > 0)
                mHandler.postDelayed(this, DELAYED);
        }
    };

    public interface PerSecondCallback {
        void onPerSecond(int leftTime);
    }

    public interface FinishCallback {
        void onFinish();
    }

    public static final class Builder {
        private WeakReference<PerSecondCallback> mPerSecondCallbackWeakRef;
        private WeakReference<FinishCallback> mFinishCallbackWeakRef;
        private int mDefaultTime = 120;

        public Builder() {
        }

        public Builder setRerSecondCallback(PerSecondCallback perSecondCallback) {
            mPerSecondCallbackWeakRef = new WeakReference<>(perSecondCallback);
            return this;
        }

        public Builder setFinishCallback(FinishCallback finishCallback) {
            mFinishCallbackWeakRef = new WeakReference<>(finishCallback);
            return this;
        }

        public Builder setTime(int time) {
            mDefaultTime = time;
            return this;
        }

        public VerificationCodeTimer build() {
            return new VerificationCodeTimer(this);
        }
    }
}
